package Chat;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.swing.text.BadLocationException;

import gui.ClientInfo;
import gui.ThreadLangNghe;
import BoBai.DanhSachBoBaiDaSX;
import NguoiChoi.*;


public class ThreadNhanTinHieuChat2 extends Thread {

	public ClientInfo ci;
	public ObjectInputStream ois;
	public ObjectOutputStream oos;
	
	public ThreadNhanTinHieuChat2(ClientInfo o) {
		setName("Chat Receive Thread");
		
		ci = o;
		
	}
	
	@Override
	public void run() {
		
		try {
			ois = new ObjectInputStream(ci.chatSocket.getInputStream());
			oos = new ObjectOutputStream(ci.chatSocket.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}

		System.out.println("Start Chat Receive Thread ...");
		
		String s;
		while(true) {
			
			try {
				s = ois.readUTF();	
				String[] sli = s.split(";");
				if(sli[0].equals("dn")){
					//dang nhap
					NguoiChoi nc = new NguoiChoi(sli[1]);
					DanhSachNguoiChoi.DSNC.add(nc);
					System.out.println(DanhSachNguoiChoi.DSNC.size());
					

					//sent 13 la bai cho slient
					if(ThreadLangNghe.soluongclient == 1){
						String chuoi = "";
						for(int i=0; i<12; i++){
							chuoi = chuoi + DanhSachBoBaiDaSX.DSBoBaiSX.get(i).toString() + ";";
						}
						chuoi = chuoi + DanhSachBoBaiDaSX.DSBoBaiSX.get(12).toString();
						
						//ds;labai1;labai2;..;labai13
						String s1 = "ds;" + chuoi;
						try {
							oos.writeUTF(s1);
							oos.flush();
							oos.reset();
						} catch (IOException e) {
							e.printStackTrace();
						}
						
					}else if(ThreadLangNghe.soluongclient == 2){
						String chuoi = "";
						for(int i=13; i<25; i++){
							chuoi = DanhSachBoBaiDaSX.DSBoBaiSX.get(i).toString() + ";";
						}
						chuoi = chuoi + DanhSachBoBaiDaSX.DSBoBaiSX.get(25).toString();
						
						//ds;labai1;labai2;..;labai13
						String s1 = "ds;" + chuoi;
						try {
							oos.writeUTF(s1);
							oos.flush();
							oos.reset();
						} catch (IOException e) {
							e.printStackTrace();
						}
						
						
					}else if(ThreadLangNghe.soluongclient == 3){
						String chuoi = "";
						for(int i=26; i<38; i++){
							chuoi = DanhSachBoBaiDaSX.DSBoBaiSX.get(i).toString() + ";";
						}
						chuoi = chuoi + DanhSachBoBaiDaSX.DSBoBaiSX.get(38).toString();
						
						//ds;labai1;labai2;..;labai13
						String s1 = "ds;" + chuoi;
						try {
							oos.writeUTF(s1);
							oos.flush();
							oos.reset();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}else if(ThreadLangNghe.soluongclient == 4){
						String chuoi = "";
						for(int i=39; i<51; i++){
							chuoi = DanhSachBoBaiDaSX.DSBoBaiSX.get(i).toString() + ";";
						}
						chuoi = chuoi + DanhSachBoBaiDaSX.DSBoBaiSX.get(51).toString();
						
						//ds;labai1;labai2;..;labai13
						String s1 = "ds;" + chuoi;
						try {
							oos.writeUTF(s1);
							oos.flush();
							oos.reset();
						} catch (IOException e) {
							e.printStackTrace();
						}
						
						//tinh toan client nao dc di truoc. sent tin hieu bat dau choi
					}
					}else if(sli[0].equals("db")){
					//danh bai
				}
				
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
	}
	
	
}
